package apresentacao;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import persistencia.Conexao;
import persistencia.DaoPapelaria;
import dominio.Papelaria;
import javax.swing.JScrollPane;

public class TelaCadastroPapelaria extends JDialog {
	
	private JTextField textField_Nome;
	private JTextField textField_Setor;
	private JTextField textField_PrecoCompra;
	private JTextField textField_Interprete;
	private JTextField textField;
	private final JPanel contentPanel = new JPanel();
	private JTextField textField_Marca;
	private JTextField textField_Especificacao;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			TelaCadastroPapelaria dialog = new TelaCadastroPapelaria();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public TelaCadastroPapelaria() {
		getContentPane().setBackground(new Color(240, 230, 140));
		setBounds(100, 100, 570, 458);
		getContentPane().setLayout(null);
		
		JLabel lbl_CadPapelaria = new JLabel("Cadastro - Papelaria");
		lbl_CadPapelaria.setHorizontalAlignment(SwingConstants.CENTER);
		lbl_CadPapelaria.setFont(new Font("Arial", Font.BOLD, 20));
		lbl_CadPapelaria.setBounds(10, 11, 534, 34);
		getContentPane().add(lbl_CadPapelaria);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBackground(new Color(238, 232, 170));
		panel.setBounds(10, 71, 296, 337);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lbl_nome = new JLabel("Nome :");
		lbl_nome.setFont(new Font("Arial", Font.BOLD, 13));
		lbl_nome.setBounds(23, 11, 54, 21);
		panel.add(lbl_nome);
		
		textField_Nome = new JTextField();
		textField_Nome.setBounds(23, 31, 248, 27);
		textField_Nome.setFont(new Font("Arial", Font.PLAIN, 12));
		textField_Nome.setColumns(10);
		panel.add(textField_Nome);
		
		JLabel lbl_descricao = new JLabel("Descri\u00E7\u00E3o :");
		lbl_descricao.setBounds(23, 74, 70, 21);
		lbl_descricao.setFont(new Font("Arial", Font.BOLD, 13));
		panel.add(lbl_descricao);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(25, 95, 246, 96);
		panel.add(scrollPane);
		
		final JTextArea textArea_Descricao = new JTextArea();
		scrollPane.setViewportView(textArea_Descricao);
		textArea_Descricao.setWrapStyleWord(true);
		textArea_Descricao.setLineWrap(true);
		textArea_Descricao.setFont(new Font("Arial", Font.PLAIN, 12));
		
		JLabel lbl_corredor = new JLabel("Corredor :");
		lbl_corredor.setBounds(23, 207, 70, 21);
		lbl_corredor.setFont(new Font("Arial", Font.BOLD, 13));
		panel.add(lbl_corredor);
		
		JLabel lbl_setor = new JLabel("Setor :");
		lbl_setor.setBounds(100, 207, 46, 21);
		lbl_setor.setFont(new Font("Arial", Font.BOLD, 13));
		panel.add(lbl_setor);
		
		final JComboBox comboBox_Corredor = new JComboBox();
		comboBox_Corredor.setBounds(23, 228, 70, 27);
		comboBox_Corredor.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6"}));
		comboBox_Corredor.setFont(new Font("Arial", Font.PLAIN, 12));
		panel.add(comboBox_Corredor);
		
		textField_Setor = new JTextField();
		textField_Setor.setBounds(100, 229, 171, 27);
		textField_Setor.setFont(new Font("Arial", Font.PLAIN, 12));
		textField_Setor.setColumns(10);
		panel.add(textField_Setor);
		
		JLabel lbl_precoCompra = new JLabel("Pre\u00E7o de Compra :");
		lbl_precoCompra.setBounds(23, 267, 123, 25);
		lbl_precoCompra.setFont(new Font("Arial", Font.BOLD, 13));
		panel.add(lbl_precoCompra);
		
		textField_PrecoCompra = new JTextField();
		textField_PrecoCompra.setFont(new Font("Arial", Font.PLAIN, 12));
		textField_PrecoCompra.setBounds(46, 290, 91, 27);
		textField_PrecoCompra.setColumns(10);
		panel.add(textField_PrecoCompra);
		
		JLabel lbl_RS = new JLabel("R$");
		lbl_RS.setBounds(23, 295, 27, 14);
		lbl_RS.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel.add(lbl_RS);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_1.setBackground(new Color(238, 232, 170));
		panel_1.setBounds(330, 71, 214, 249);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lbl_largura = new JLabel("Largura :");
		lbl_largura.setFont(new Font("Arial", Font.BOLD, 13));
		lbl_largura.setBounds(23, 191, 64, 20);
		panel_1.add(lbl_largura);
		
		JLabel lbl_altura = new JLabel("Altura :");
		lbl_altura.setFont(new Font("Arial", Font.BOLD, 13));
		lbl_altura.setBounds(127, 191, 46, 20);
		panel_1.add(lbl_altura);
		
		final JSpinner spinner_Largura = new JSpinner();
		spinner_Largura.setFont(new Font("Arial", Font.PLAIN, 12));
		spinner_Largura.setBounds(23, 212, 64, 26);
		panel_1.add(spinner_Largura);
		
		final JSpinner spinner_Altura = new JSpinner();
		spinner_Altura.setFont(new Font("Arial", Font.PLAIN, 12));
		spinner_Altura.setBounds(127, 212, 64, 26);
		panel_1.add(spinner_Altura);
		
		JLabel lbl_x = new JLabel("       x");
		lbl_x.setBounds(86, 218, 32, 14);
		panel_1.add(lbl_x);
		
		JLabel lbl_tipo = new JLabel("Tipo :");
		lbl_tipo.setFont(new Font("Arial", Font.BOLD, 13));
		lbl_tipo.setBounds(23, 74, 46, 20);
		panel_1.add(lbl_tipo);
		
		final JComboBox comboBox_Tipo = new JComboBox();
		comboBox_Tipo.setModel(new DefaultComboBoxModel(new String[] {"Caderno", "Agenda", "Resma de papel"}));
		comboBox_Tipo.setBounds(23, 94, 109, 26);
		panel_1.add(comboBox_Tipo);
		
		JLabel lbl_Marca = new JLabel("Marca :");
		lbl_Marca.setFont(new Font("Arial", Font.BOLD, 13));
		lbl_Marca.setBounds(23, 11, 64, 20);
		panel_1.add(lbl_Marca);
		
		textField_Marca = new JTextField();
		textField_Marca.setFont(new Font("Arial", Font.PLAIN, 12));
		textField_Marca.setBounds(23, 31, 168, 26);
		panel_1.add(textField_Marca);
		textField_Marca.setColumns(10);
		
		JLabel lbl_especificacao = new JLabel("Especifica\u00E7\u00E3o :");
		lbl_especificacao.setFont(new Font("Arial", Font.BOLD, 13));
		lbl_especificacao.setBounds(23, 136, 109, 20);
		panel_1.add(lbl_especificacao);
		
		textField_Especificacao = new JTextField();
		textField_Especificacao.setFont(new Font("Arial", Font.PLAIN, 12));
		textField_Especificacao.setBounds(23, 160, 168, 26);
		panel_1.add(textField_Especificacao);
		textField_Especificacao.setColumns(10);
		
		JButton btn_Salvar = new JButton("Salvar");
		btn_Salvar.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){ 
				Papelaria papelaria = new Papelaria();
				Connection conn = null;
				try
				{
					Conexao bd = new Conexao();
					DaoPapelaria dao = new DaoPapelaria();
					conn = bd.obtemConexao();
				
					conn.setAutoCommit(false);
				
					
					papelaria.setNome(textField_Nome.getText());
					papelaria.setIdtipoproduto(3);
					papelaria.setSetor(textField_Setor.getText());
					papelaria.setDescricao(textArea_Descricao.getText());
					papelaria.setPrecocompra(Float.parseFloat(textField_PrecoCompra.getText()));
					papelaria.setCorredor((String)comboBox_Corredor.getSelectedItem().toString());
					papelaria.setMarca(textField_Marca.getText());
					papelaria.setEspecificacao(textField_Especificacao.getText());
					papelaria.setTipo((String)comboBox_Tipo.getSelectedItem().toString());
					papelaria.setAltura((int)spinner_Altura.getValue());
					papelaria.setLargura((int)spinner_Largura.getValue());
					
					
					dao.incluir(conn, papelaria);
					conn.commit();
					JOptionPane.showMessageDialog(rootPane, "Item cadastrado com sucesso!");
					
					textField_Nome.setText("");
					textField_Setor.setText("");
					textArea_Descricao.setText("");
					textField_PrecoCompra.setText("");
					comboBox_Corredor.setSelectedItem("1");
					textField_Especificacao.setText("");
					comboBox_Tipo.setSelectedItem("Caderno");
					textField_Marca.setText("");
					spinner_Altura.setValue(0);
					spinner_Largura.setValue(0);
					
					
				}
				catch (Exception ex) {
					ex.printStackTrace();
					if (conn != null) {
						try {
							conn.rollback();
						}
						catch (SQLException e1) {
							System.out.print(e1.getStackTrace());
						}
					}
				}
				finally {
					if (conn != null) {
						try {
							conn.close();
						}
						catch (SQLException e1) {
							System.out.print(e1.getStackTrace());
						}
					}
				}
			}
		});	
		
		btn_Salvar.setFont(new Font("Arial", Font.BOLD, 14));
		btn_Salvar.setBounds(386, 375, 84, 33);
		getContentPane().add(btn_Salvar);
		
		JButton btn_Voltar = new JButton("Voltar");
		btn_Voltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btn_Voltar.setFont(new Font("Arial", Font.PLAIN, 12));
		btn_Voltar.setBounds(474, 375, 70, 33);
		getContentPane().add(btn_Voltar);
		}
}

